{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": 1,
   "id": "a05045a8",
   "metadata": {},
   "outputs": [],
   "source": [
    "import rdata\n",
    "import os\n",
    "import fastkde\n",
    "import pandas as pd\n",
    "import numpy as np\n",
    "import matplotlib.pyplot as plt\n",
    "import matplotlib.colors as mcolors\n",
    "import json"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "4b1ede45",
   "metadata": {},
   "outputs": [],
   "source": [
    "# read in data\n",
    "nm_pos = rdata.parser.parse_file(\"v7acs_nm_pos.rda\")\n",
    "col_pos = rdata.parser.parse_file(\"v7acs_col_pos.rda\")\n",
    "mag_pos = rdata.parser.parse_file(\"v7acs_mag_pos.rda\")\n",
    "col_err_pos = rdata.parser.parse_file(\"v7acs_col_err_pos.rda\")\n",
    "\n",
    "# convert r data object to python \n",
    "nm_pos = rdata.conversion.convert(nm_pos)\n",
    "col_pos = rdata.conversion.convert(col_pos)\n",
    "mag_pos = rdata.conversion.convert(mag_pos)\n",
    "col_err_pos = rdata.conversion.convert(col_err_pos)\n",
    "\n",
    "# extract data\n",
    "nm_pos_list = nm_pos['nm_pos']\n",
    "col_pos_list = col_pos['col_pos']\n",
    "mag_pos_list = mag_pos['mag_pos']\n",
    "col_err_pos_list = col_err_pos['col_err_pos']"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "2f97d8da",
   "metadata": {},
   "outputs": [],
   "source": [
    "nm_pos_df = pd.concat(nm_pos_list)\n",
    "col_pos_df = pd.concat(col_pos_list)\n",
    "mag_pos_df = pd.concat(mag_pos_list)\n",
    "col_err_pos_df = pd.concat(col_err_pos_list)\n",
    "\n",
    "nm_pos_df = nm_pos_df[(nm_pos_df['x'] < 1) & (nm_pos_df['x'] > 0) & (nm_pos_df['y'] < 1) & (nm_pos_df['y'] > 0)]\n",
    "mag_pos_df = mag_pos_df[(mag_pos_df['x'] < 1) & (mag_pos_df['x'] > 0) & (mag_pos_df['y'] < 1) & (mag_pos_df['y'] > 0)]\n",
    "col_pos_df = col_pos_df[(col_pos_df['x'] < 1) & (col_pos_df['x'] > 0) & (col_pos_df['y'] < 1) & (col_pos_df['y'] > 0)]\n",
    "col_err_pos_df = col_err_pos_df[(col_err_pos_df['x'] < 1) & (col_err_pos_df['x'] > 0) & (col_err_pos_df['y'] < 1) & (col_err_pos_df['y'] > 0)]\n",
    "\n",
    "x_nm = np.array(nm_pos_df['x'])\n",
    "y_nm = np.array(nm_pos_df['y'])\n",
    "\n",
    "x_col = np.array(col_pos_df['x'])\n",
    "y_col = np.array(col_pos_df['y'])\n",
    "\n",
    "x_mag = np.array(mag_pos_df['x'])\n",
    "y_mag = np.array(mag_pos_df['y'])\n",
    "\n",
    "x_col_err = np.array(col_err_pos_df['x'])\n",
    "y_col_err = np.array(col_err_pos_df['y'])\n",
    "\n",
    "PDF_nm = fastkde.pdf(x_nm, y_nm, var_names = ['x', 'y'], num_points = 1025)\n",
    "PDF_col = fastkde.pdf(x_col, y_col, var_names = ['x', 'y'], num_points = 1025)\n",
    "PDF_mag = fastkde.pdf(x_mag, y_mag, var_names = ['x', 'y'], num_points = 1025)\n",
    "PDF_col_err = fastkde.pdf(x_col_err, y_col_err, var_names = ['x', 'y'], num_points = 1025)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "cbcb659e",
   "metadata": {},
   "outputs": [],
   "source": [
    "# Assuming PDF_nm, PDF_mag, PDF_col, PDF_col_err are xarray DataArray objects\n",
    "fig, axs = plt.subplots(2, 2, figsize=(10, 10), sharex=True, sharey=True)\n",
    "\n",
    "# Define a normalization for the color scale\n",
    "norm = mcolors.Normalize(vmin=0, vmax=1)\n",
    "\n",
    "# Plot each DataArray with the same normalization\n",
    "im1 = PDF_nm.plot(ax=axs[0, 0])\n",
    "im2 = PDF_mag.plot(ax=axs[0, 1])\n",
    "im3 = PDF_col.plot(ax=axs[1, 0])\n",
    "im4 = PDF_col_err.plot(ax=axs[1, 1])\n",
    "\n",
    "fig.subplots_adjust(right=0.85)\n",
    "cbar_ax = fig.add_axes([0.88, 0.15, 0.03, 0.7])  # [left, bottom, width, height]\n",
    "\n",
    "axs[0,0].set_title('No Mark')\n",
    "axs[0,1].set_title('Magnitude')\n",
    "axs[1,0].set_title('Color')\n",
    "axs[1,1].set_title('Color + Error')\n",
    "\n",
    "\n",
    "# Create a colorbar in the specified axis\n",
    "cbar = fig.colorbar(im1, cax=cbar_ax)\n",
    "\n",
    "plt.show()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 26,
   "id": "e3685b49",
   "metadata": {},
   "outputs": [
    {
     "name": "stderr",
     "output_type": "stream",
     "text": [
      "/Users/davidolohowski/anaconda3/lib/python3.9/site-packages/fastkde/fastKDE.py:1154: FutureWarning: Support for multi-dimensional indexing (e.g. `obj[:, None]`) is deprecated and will be removed in a future version.  Convert to a numpy array before indexing instead.\n",
      "  inputVariables = npy.array(var0[npy.newaxis, :])\n",
      "/Users/davidolohowski/anaconda3/lib/python3.9/site-packages/fastkde/fastKDE.py:1180: FutureWarning: Support for multi-dimensional indexing (e.g. `obj[:, None]`) is deprecated and will be removed in a future version.  Convert to a numpy array before indexing instead.\n",
      "  varn = npy.array(var_args[i][npy.newaxis, :])\n"
     ]
    }
   ],
   "source": [
    "np.random.seed(537380)\n",
    "nm_sim_list = []\n",
    "mag_sim_list = []\n",
    "col_sim_list = []\n",
    "col_err_sim_list = []\n",
    "for j in range(100):\n",
    "    block_idx = np.random.choice(range(1800), 1800, replace=True)\n",
    "\n",
    "    # Create resampled blocks\n",
    "    rs_blocks = [range(100 * x, 100 * (x+1)) for x in block_idx]\n",
    "    rs_blocks_flat = [item for sublist in rs_blocks for item in sublist]\n",
    "\n",
    "    X_nm_j = pd.concat([nm_pos_list[i] for i in rs_blocks_flat], ignore_index=True)\n",
    "    X_nm_j = X_nm_j[(X_nm_j['x'] < 1) & (X_nm_j['x'] > 0) & (X_nm_j['y'] < 1) & (X_nm_j['y'] > 0)]\n",
    "\n",
    "    X_mag_j = pd.concat([mag_pos_list[i] for i in rs_blocks_flat], ignore_index=True)\n",
    "    X_mag_j = X_mag_j[(X_mag_j['x'] < 1) & (X_mag_j['x'] > 0) & (X_mag_j['y'] < 1) & (X_mag_j['y'] > 0)]\n",
    "    \n",
    "    X_col_j = pd.concat([col_pos_list[i] for i in rs_blocks_flat], ignore_index=True)\n",
    "    X_col_j = X_col_j[(X_col_j['x'] < 1) & (X_col_j['x'] > 0) & (X_col_j['y'] < 1) & (X_col_j['y'] > 0)]\n",
    "    \n",
    "    X_col_err_j = pd.concat([col_err_pos_list[i] for i in rs_blocks_flat], ignore_index=True)\n",
    "    X_col_err_j = X_col_err_j[(X_col_err_j['x'] < 1) & (X_col_err_j['x'] > 0) & (X_col_err_j['y'] < 1) & (X_col_err_j['y'] > 0)]\n",
    "\n",
    "\n",
    "    PDF_nm_sim = fastkde.pdf(X_nm_j['x'], X_nm_j['y'], var_names = ['x', 'y'], num_points = 1025)\n",
    "    PDF_mag_sim = fastkde.pdf(X_mag_j['x'], X_mag_j['y'], var_names = ['x', 'y'], num_points = 1025)\n",
    "    PDF_col_sim = fastkde.pdf(X_col_j['x'], X_col_j['y'], var_names = ['x', 'y'], num_points = 1025)\n",
    "    PDF_col_err_sim = fastkde.pdf(X_col_err_j['x'], X_col_err_j['y'], var_names = ['x', 'y'], num_points = 1025)\n",
    "\n",
    "    \n",
    "    nm_pdf = PDF_nm_sim.values.tolist()\n",
    "    nm_sim_list.append(nm_pdf)\n",
    "    mag_pdf = PDF_mag_sim.values.tolist()\n",
    "    mag_sim_list.append(mag_pdf)\n",
    "    col_pdf = PDF_col_sim.values.tolist()\n",
    "    col_sim_list.append(col_pdf)\n",
    "    col_err_pdf = PDF_col_err_sim.values.tolist()\n",
    "    col_err_sim_list.append(col_err_pdf)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 42,
   "id": "a30c1115",
   "metadata": {},
   "outputs": [],
   "source": [
    "FILE_PATH = './col_err_sim_pos.json'\n",
    "\n",
    "with open(FILE_PATH, 'w') as output_file:\n",
    "\tjson.dump(col_err_sim_list, output_file, indent=2)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 51,
   "id": "a588fc46",
   "metadata": {},
   "outputs": [],
   "source": [
    "FILE_PATH = './v11acs_col_err_pdf.json'\n",
    "\n",
    "with open(FILE_PATH, 'w') as output_file:\n",
    "\tjson.dump(PDF_col_err.values.tolist(), output_file, indent=1)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "03c588a7",
   "metadata": {},
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3 (ipykernel)",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.9.12"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 5
}
